package com.Ugent.Minerva;

import java.io.Serializable;
import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class CourseList extends ListActivity {
	TextView myCourses;
	TextView gebruiker;
	private CourseAdapter adapter;
	private ArrayList<Course> courseList = null;
	private ArrayList<Tool> toolList = new ArrayList<Tool>();
	private Student s1 = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.courselistmain);
	  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	  
	  //get courselist
	  @SuppressWarnings("unchecked")
	  ArrayList<Course> serializableExtra = (ArrayList<Course>) getIntent().getSerializableExtra("courseList");
	  courseList = serializableExtra;
	  //get student object
	  s1 = (Student) getIntent().getSerializableExtra("student");
	  
	  //make own adapter to display courses
	  this.adapter = new CourseAdapter (this, R.layout.courselist, courseList);
	  setListAdapter(this.adapter);
	  ListView lv = getListView();

	  myCourses = (TextView) findViewById(R.id.myCourses);  
	  gebruiker = (TextView) findViewById(R.id.gebruiker);
	  gebruiker.setText(s1.getName());
	  
	  //make courselinks clickable
	  lv.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {	  
        	  Course a = courseList.get(position);
        	  
        	  //get toollist for a specific course
        	  toolList = Facade.getTools(position, courseList);
              Intent intent=new Intent(getApplicationContext(),Tools.class);
              intent.putExtra("course", (Serializable) a); 
              intent.putExtra("student", (Serializable) s1); 
              intent.putExtra("tools", (Serializable) toolList); 
              startActivity(intent); 
        }
        });
	}
	
	public void clickHandler(View v){    	
	    
		startActivity(new Intent(getApplicationContext(), Account.class));
	
	} 
	
	//own adapter
	private class CourseAdapter extends ArrayAdapter<Course> {

    private ArrayList<Course> items;

    public CourseAdapter(Context context, int textViewResourceId, ArrayList<Course> items) {
            super(context, textViewResourceId, items);
            this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.courselist, null);
            }
            Course c = items.get(position);
            if (c != null) {
                    TextView tt = (TextView) v.findViewById(R.id.toptext);
                    TextView bt = (TextView) v.findViewById(R.id.bottomtext);
                    if (tt != null) {
                          tt.setText(c.getTitle());                            }
                    if(bt != null){
                          bt.setText(c.getTutor());
                    }
            }
            return v;
    }
}
}

